<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class ColumnController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view("admin.column.index");
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getcolumn(Request $request){
        $input = $request->input();
        $start = $input['page']*$input['limit']-$input['limit'];
        $end = $input['page']*$input['limit'];
        $data = [
            'code'=>0,
            'msg'=>'',
            'count'=>DB::table("column")->count(),
            'data'=>DB::table("column")->offset($start)->limit($end)->get()
        ];
        return $data;
    }
    public function create(Request $request)
    {
        $input = $request->input();
        if($input['id'] != 0){
            $data = DB::table("column")->where("id",$input['id'])->first();
        }else{
            $data = '';
        }
        return view("admin.column.add",['data'=>$data]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input = $request->input();
        $column_data = [
            'name'=>$input['name'],
            'display_name'=>$input['display_name'],
            'pid' => $input['pid'],
        ];
        if($input['luyou']){
            $column_data['luyou'] = $input['luyou'];
        }
        $re = DB::table("column")->insert($column_data);
        if($re){
            return view("welcome",['id'=>1,'msg'=>'添加成功']);
        }else{
            return view("welcome",['id'=>2,'msg'=>'添加失败']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $column = DB::table("column")->where("pid",0)->get();
        $data = DB::table("column")->where("id",$id)->first();
        return view("admin.column.edit",['data'=>$data,'column'=>$column]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $input = $request->input();
        $column_data = [
            'name'=>$input['name'],
            'display_name'=>$input['display_name'],
            'pid' => $input['pid'],
        ];
        if($input['luyou']){
            $column_data['luyou'] = $input['luyou'];
        }else{
            $column_data['luyou'] = '';
        }
        $re = DB::table("column")->where("id",$id)->update($column_data);
        if($re){
            return view("welcome",['id'=>1,'msg'=>'编辑成功']);
        }else{
            return view("welcome",['id'=>2,'msg'=>'编辑失败']);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
